﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmRMConfirmOut : Form
    {
        public FrmRMConfirmOut()
        {
            InitializeComponent();
        }
 
        DataTable dt;
        
        string BillNo = string.Empty;
        int rowNo = 0;
      
        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    var txtbillnoList = txtBillNo.Text.Split('-');
                    if (txtbillnoList.Length >= 2)
                    {
                        BillNo = txtbillnoList[0];
                        rowNo = Convert.ToInt32(txtbillnoList[1]);

                    }
                    else
                    {
                        MessageBox.Show("请选择正确的单号!");
                        txtBillNo.Text = "";
                        return;
                    }
                    dt = Bll_RMOut.getRMRows(BillNo, rowNo);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        lblRow.Text = dt.Rows[0]["RowNo"].ToString();
                        lblQty.Text = dt.Rows[0]["RealQty"].ToString();

                    }
                    else
                    {
                        MessageBox.Show("请选择正确的单号!");
                        txtBillNo.Text = "";
                        return;
                    }
                }
                catch 
                {
                    MessageBox.Show("数据有错误!");
                    txtBillNo.Text = "";
                    return;
                }
            }
        }

        private void FrmRMIn_Load(object sender, EventArgs e)
        {

            txtBillNo.Focus();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var dt = Bll_RMOut.getRMRows(BillNo, rowNo);
                var dtdetail = Bll_RMOut.getRMDetails(BillNo, rowNo);
                StringBuilder sb = new StringBuilder();
                //var isFinish = false;
                var isRowFinish = false;
                if (dt != null && dt.Rows.Count > 0)
                {
                    int rmDetailOverCount = 0;
                    if (dtdetail != null && dtdetail.Rows.Count > 0)
                    {
                        rmDetailOverCount = dtdetail.Rows.Count;
                    }
                    decimal planQtyCount = 0;
                    decimal realQtyCount = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        decimal planQty = Convert.ToDecimal(dr["PlanQty"]);
                        decimal realQty = Convert.ToDecimal(dr["RealQty"]);
                        if ((dr["Status"].ToString() == "0"))
                        {
                            var resRow = Bll_RMOut.UpdateRMRow(dr["BillNo"].ToString(), dr["RowNo"].ToString());
                            if (resRow)
                            {
                                MessageBox.Show("该订单行发货确认" + BillNo + "已完成!");
                            }
                        }
                        planQtyCount = planQtyCount + planQty;
                        realQtyCount = realQtyCount + realQty;
                    }

                    sb.AppendLine("\r\n出货次数:" + rmDetailOverCount + "\r\n出货数量:" + realQtyCount + "/" + planQtyCount);

                    var rowDate = Bll_RMOut.getRMRowsOver(BillNo);
                    if (!((rowDate != null) && (rowDate.Rows.Count > 0))) //每当有行表状态为完成时，判断是否该订单所有行数据已经完成，如果完成，则修改该订单状态为完成 并提示该订单完成。
                    {

                        var res = Bll_RMOut.UpdateRM(BillNo);
                        //if (res)
                        //{

                        //    MessageBox.Show("该订单" + billNo + "已完成!");
                        //}
                    }

                }

                lab_msg.Text = "订单" + BillNo + "行" + rowNo.ToString() + sb.ToString();
            }
            catch 
            {
                lab_msg.Text = "发货确认失败!";
            }
        }
      

       
    }
}